﻿@{
    ViewBag.Title = "分类列表";

}
@section styles{
    <link href="~/static/css/Ztree.css" type="text/css" rel="stylesheet" />
    <link href="~/static/css/metroStyle.css" type="text/css" rel="stylesheet" />
    <link href="~/static/css/public.css" type="text/css" rel="stylesheet" />
    <link href="~/static/css/client.css" type="text/css" rel="stylesheet" />
}
<style>
    a {
        text-decoration: none;
    }
</style>
<div class="layui-fluid">
    <div class="layui-row">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">@ViewBag.Title</div>
                <div class="layui-card-body">
                    <blockquote class="layui-elem-quote">注意：修改时默认选择第一个进行修改</blockquote>
                </div>
                <div class="layui-card-body layui-col-md6">
                    <div class="layui-row" style="padding-left:5px;">
                        <div class="layui-btn-group parent-type layui-col-md4">
                            <button class="layui-btn layui-btn-sm" data-type="Create" data-isparent="true">
                                <i class="layui-icon">&#xe654;</i>
                            </button>
                            <button class="layui-btn layui-btn-sm" data-type="Edit" data-isparent="true">
                                <i class="layui-icon">&#xe642;</i>
                            </button>
                            @*<button class="layui-btn layui-btn-sm" data-type="Delete" data-isparent="true">
                                    <i class="layui-icon">&#xe640;</i>
                                </button>*@
                            <button class="layui-btn layui-btn-sm">
                                <i class="layui-icon">&#xe602;</i>
                            </button>
                        </div>
                        <div class="layui-col-md8">
                            <div class="demoTable">
                                <div class="layui-inline">
                                    <input class="layui-input" name="keyword" id="pname" placeholder="分类名称" autocomplete="off">
                                </div>
                                <button class="layui-btn search" data-type="reload" data-isparent="true">搜索</button>
                            </div>
                        </div>
                    </div>

                    <table class="layui-hide layui-table" id="layui-table1" lay-filter="Event"></table>
                </div>
                <div class="layui-card-body layui-col-md6">
                    <div class="layui-row" style="padding-left:5px;">
                        <div class="layui-btn-group parent-type layui-col-md4">
                            <button class="layui-btn layui-btn-sm" data-type="Create" data-isparent="false">
                                <i class="layui-icon">&#xe654;</i>
                            </button>
                            <button class="layui-btn layui-btn-sm" data-type="Edit" data-isparent="false">
                                <i class="layui-icon">&#xe642;</i>
                            </button>
                            <button class="layui-btn layui-btn-sm" data-type="Delete" data-isparent="false">
                                <i class="layui-icon">&#xe640;</i>
                            </button>
                            <button class="layui-btn layui-btn-sm">
                                <i class="layui-icon">&#xe602;</i>
                            </button>
                        </div>
                        <div class="layui-col-md8">
                            <div class="demoTable">

                                <div class="layui-inline">
                                    <input class="layui-input" name="keyword" id="cname" placeholder="分类名称" autocomplete="off">
                                </div>
                                <button class="layui-btn search" data-type="reload" data-isparent="false">搜索</button>
                            </div>
                        </div>
                    </div>

                    <table class="layui-hide layui-table" id="layui-table2" lay-filter="table2"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="Layer_ajax"></div>
<script type="text/html" id="operating">
    <div class="layui-form">
        {{#  if(d.isactive){ }}
        <input type="checkbox" name="zzz" lay-skin="switch" checked value="{{d.id }}" lay-filter="checkbox" lay-text="启用|停用">
        {{#  }else{ }}
        <input type="checkbox" name="zzz" lay-skin="switch" value="{{d.id }}" lay-filter="checkbox" lay-text="启用|停用">
        {{# } }}
    </div>
</script>
@section scripts{
    <script src="~/static/js/jquery.ztree.core.js" type="text/javascript"></script>
    <script src="~/static/js/jquery.ztree.excheck.js" type="text/javascript"></script>
    <script src="~/static/js/jquery.ztree.exedit.js" type="text/javascript"></script>
    <script src="~/js/main.js"></script>
    <script type="text/javascript">
        var tableH = $(".layui-table-container").height();
        layui.use(['form', 'layedit', 'laydate', 'tree', 'table', 'layer', 'element', 'upload'], function () {
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate
                , table = layui.table
                , upload = layui.upload;

            table.render({
                elem: "#layui-table1",
                url: '/ShopType/GetShopTypeList',
                cellMinWidth: 26,
                cols: [[
                    { type: 'checkbox' },
                    { field: 'typeName', title: '分类名称', event: 'setSign' },
                    { field: 'isactive', title: '启用状态', toolbar: '#operating', width: 100 }
                    //{ title: '操作', width: 120, align: 'center', toolbar: '#barDemo' }
                ]],
                id: "table1",
                //limits: [1, 2, 3, 5, 6],
                page: false,
                done: function (res, curr, count) {
                    if (res.data.length > 0) {
                        var id = res.data[0].id;
                        table.render({
                            elem: "#layui-table2",
                            url: '/ShopType/GetShopTypeList2',
                            where: { id: id },
                            cellMinWidth: 26,
                            cols: [[
                                { type: 'checkbox' },
                                { field: 'typeName', title: '分类名称' },
                                { field: 'isactive', title: '启用状态', toolbar: '#operating', width: 100 }

                            ]],
                            id: "table2",
                            page: false
                        });
                    }
                }
            });

            table.on('tool(Event)', function (obj) {
                var data = obj.data;
                if (obj.event === 'setSign') {
                    table.reload('table2', {
                        where: {
                            id: data.id,
                        }
                    })
                }
            });


            //监听状态开关
            form.on('switch(checkbox)', function (data) {
                var id = data.value;
                var prompt = ""
                if (data.elem.checked) {
                    prompt = "分类已启用！"
                } else {
                    prompt = "分类已停用！"
                }

                $.get("/ShopType/SwitchActive?id=" + id + "&active=" + data.elem.checked, function (r) {
                    if (r.success) {
                        layer.msg(prompt, {
                            icon: 1,
                            time: 2000
                        });
                        table.reload('table2');
                    } else {
                        layer.msg("操作失败", {
                            icon: 2,
                            time: 2000
                        });
                    }

                })
            });

            var $ = layui.$, active = {
                Create: function (isparent) {
                    LayerOpen("添加分类", "/ShopType/Create?istop=" + isparent);
                }
                , Edit: function (isparent) {
                    var checkStatus;
                    if (isparent) {
                        checkStatus = table.checkStatus('table1')
                    } else {
                        checkStatus = table.checkStatus('table2')
                    }
                    var data = checkStatus.data;
                    if (data.length > 0) {
                        LayerOpen("编辑分类-" + data[0].typeName, "/ShopType/Edit?id=" + data[0].id);
                    }
                    else {
                        layer.msg("请选择一个信息进行编辑！")
                    }
                }
                , Delete: function (isparent) {
                    var checkStatus;
                    if (isparent) {
                        checkStatus = table.checkStatus('table1')
                    } else {
                        checkStatus = table.checkStatus('table2')
                    }
                    var data = checkStatus.data;
                    if (data.length > 0) {
                        var ids = new Array();
                        var names = new Array();
                        for (var i = 0; i < data.length; i++) {
                            ids.push(data[i].id);
                            names.push(data[i].typeName);
                        }
                        layer.confirm('确认删除：[' + names.join("|") + "] 吗？", function (index) {
                            $.post("/ShopType/Delete", { ids: ids.join("|") }, function (obj, successInfo) {
                                CommonAjaxCallback(obj, function () {
                                    table.reload('idTest', {
                                        page: {
                                            curr: 1 //重新从第 1 页开始
                                        }
                                    })
                                }, successInfo)
                            })
                            layer.close(index);
                        });
                    }
                    else {
                        layer.msg("请至少选择一个分类信息！")
                    }
                }
            };

            $('.parent-type .layui-btn').on('click', function () {
                var type = $(this).data('type');

                var isparent = $(this).data('isparent');
                active[type] ? active[type].call(this, isparent) : '';
            });

            $(".search").on('click', function () {
                var isparent = $(this).data('isparent');
                if (isparent) {
                    table.reload('table1', {
                        where: { keyword: $('#pname').val() }
                    })
                } else {
                    table.reload('table2', {
                        where: { keyword: $('#cname').val() }
                    })
                }

            });

            function LayerOpen(title, url) {
                var open = layer.open({
                    type: 2,
                    title: title,
                    shadeClose: true,
                    shade: false,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['600px', '450px'],
                    content: url,
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index)//确定两个页面的父子关系
                    },
                    end: function () {
                        //销毁后重新加载
                        table.reload('table1', {
                        })
                    }
                });
            }
        });


        $("#layui-Area").click(function () {
            $(".layui-Area .layui-Area-hide").toggleClass("layui-Area-show")
            $(this).parent(".layui-form-select").toggleClass("layui-form-selected");
        });

        $(".layui-Area").mouseleave(function () {
            $(".layui-Area .layui-Area-hide").removeClass("layui-Area-show")
        })

    </script>
}
